
import bpy



def f_从集合中拷贝物体(objects, 是否实例=True):
	objNum = len(objects)
	newObj = []
	父对象loc = [-1]*objNum
	父对象 = [None]*objNum

	objs = objects
	index = 0
	for obj in objs:
		'''新物体 = obj.copy()
		if 是否实例 == False:
			newData = obj.data.copy()
			新物体.data = newData
		newObj.append(新物体)'''

		父对象[index] = obj.parent

		if obj.parent != None :
			sonIndex = 0
			for 对比obj in objs:
				if obj.parent == 对比obj :
					父对象loc[sonIndex] = sonIndex
					break

		index += 1

	for i in objNum:
		if 父对象loc[i] >= 0 :
			newObj[i].parent = objs[ 父对象loc[i] ]
		elif 父对象[i] != None:
			newObj[i].parent = 父对象[i]

	return newObj














def f_拷贝物体父子链接(objects, newObj):
	objNum = len(objects)
	父对象loc = [-1]*objNum
	父对象 = [None]*objNum

	objs = objects
	index = 0
	for obj in objs:
		父对象[index] = obj.parent

		if obj.parent != None :
			sonIndex = 0
			for 对比obj in objs:
				if obj.parent == 对比obj and sonIndex != index :
					父对象loc[index] = sonIndex
					break
				sonIndex += 1

		index += 1
	#print('** 父对象loc = ', 父对象loc)
	#print('** 父对象 = ', 父对象)
	for i in range(objNum):
		if 父对象loc[i] >= 0 :
			#print('父对象loc[i] = ', 父对象loc[i])
			#print('newObj[ 父对象loc[i] ] = ', newObj[ 父对象loc[i] ])
			#print('newObj[i].parent = ', newObj[i].parent)
			newObj[i].parent = newObj[ 父对象loc[i] ]
		elif 父对象[i] != None:
			newObj[i].parent = 父对象[i]









def f_拷贝物体约束修改器到新物体上(objects, newObj):
	num = len(objects)
	for i in range(num):
		obj = objects[i]
		tagObj = newObj[i]

		for 修改器 in obj.constraints:
			pass
	pass





def f_ops_从集合中拷贝物体(集合, 是否实例=True):
	newObj = []

	bpy.ops.object.select_all(action='DESELECT')
	for obj in bpy.context.selected_objects :
		obj.select_set(True)
	
	if 是否实例 :
		bpy.ops.object.duplicate_move_linked()
	else:
		bpy.ops.object.duplicate_move()
	#objs = 集合.objects
	#for obj in objs:









def f_拷贝网格数据(源mesh, 目标mesh, 面索引=[]):
	索引映射 = []
	
	loops = 源mesh.loops
	源顶点 = 源mesh.vertices
	uv_layers = 源mesh.uv_layers.active.data
	

	


	UV索引 = []
	顶点索引 = []
	
	索引添加偏移 = 0
	循环添加偏移 = 0
	映射索引 = {}
	循环映射 = {}

	多边形组 = []
	for 多边形ID in 面索引 :
		多边形组.append(源mesh.polygons[多边形ID])
		
	索引数量 = 0
	for 多边形 in 多边形组:
		索引数量 += 多边形.loop_total

		_vIndex = []
		_uvIndex = []
		for i in range(多边形.loop_total):
			索引 = 多边形.loop_start + i

			vertex_index = loops[索引].vertex_index
			
			_vIndex.append(vertex_index)
			_uvIndex.append(索引)
			if (vertex_index in 映射索引.keys()) == False :
				映射索引[vertex_index] = 索引添加偏移
				索引添加偏移 += 1

			#print('多边形.loop_start = ', 多边形.loop_start)
			if (loops[索引].index in 循环映射.keys()) == False:
				循环映射[loops[索引].index] = 循环添加偏移
				循环添加偏移 += 1
			

		顶点索引.append(_vIndex)
		UV索引.append(_uvIndex)

	#for loop in loops :
		

	vertices = [(0,0,0)]*len(映射索引)
	faces = []
	uv = []

	print('目标mesh.uv_layers = ', 循环映射, 循环映射.keys())

	
	#源网格索引映射到目标网格索引
	for 面索引组 in 顶点索引:
		_面索引_ = []
		for 面索引 in 面索引组:
			_面索引_.append( 映射索引[面索引] )
		faces.append(_面索引_)

	#源网格顶点映射到目标网格顶点
	for key in 映射索引:
		vertices[ 映射索引[key] ] = 源顶点[key].co
	#print('faces = ', faces)
	#print('vertices = ', vertices)
	#print('源mesh.uv_layers = ', 源mesh.uv_layers)
	目标mesh.from_pydata(vertices=vertices, edges=[], faces=faces)


	#源网格UV映射到目标网格中
	for uvLayer in 源mesh.uv_layers:
		目标mesh.uv_layers.new(name=uvLayer.name)
		目标mesh.uv_layers[-1].data.update()
		#print('目标mesh.uv_layers[-1].data = ', 目标mesh.uv_layers[-1].data)
	
	for uv层 in range(len(目标mesh.uv_layers)):
		for 源k in 循环映射.keys():
			目标UV_ID = 循环映射[源k]
			#print('源mesh.uv_layers[uv层].data[源UV_ID].uv = ', 目标mesh.uv_layers[uv层].data, uv层, 目标UV_ID, 源k)
			#print('源mesh.uv_layers[uv层].data[源UV_ID].uv = ', 源mesh.uv_layers[uv层].data)
			#print('源mesh.uv_layers[uv层].data[源UV_ID].uv = ', 源mesh.uv_layers[uv层].data[源UV_ID].uv, k)
			#print('源mesh.uv_layers[uv层].data[源UV_ID].uv = ', 目标mesh.uv_layers[uv层].data)
			目标mesh.uv_layers[uv层].data[目标UV_ID].uv = 源mesh.uv_layers[uv层].data[源k].uv
			

	#源网格材质映射到目标网格中

	
	



